package rexsee.dialog;

import rexsee.core.browser.Browser;
import rexsee.core.lang.RexseeLanguage;
import rexsee.core.progress.RexseeProgressCover;
import rexsee.core.widget.DivDialog;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.Html;
import android.view.Gravity;
import android.view.View;
import android.widget.Chronometer;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ChronometerDialog extends DivDialog {

	private final Runnable mTimeOutRunnable;

	private final LinearLayout mContainer;
	private final TextView mElapsed;
	private final TextView mLabel;
	private final TextView mTotal;
	private final TextView mRest;

	private final Chronometer mMeter;

	public ChronometerDialog(Browser browser, String style, final int timeout, final Runnable timeOutRunnable, boolean showTotal, boolean showElapse, boolean showRest, final String highlightColor) {

		super(browser, null, "width:wrapcontent;height:wrapcontent;window-cancelable:false;window-outside-cancelable:false;window-modeless:true;window-moveable:true;window-dim-amount:0;" + style);
		mTimeOutRunnable = timeOutRunnable;

		int gravity = styleSheet.getGravity();
		final int color = Color.parseColor(styleSheet.color);

		mTotal = new TextView(mContext);
		mTotal.setGravity(gravity);
		mTotal.setText(Html.fromHtml(RexseeLanguage.PREFIX_TIME_TOTAL + "<font color=" + highlightColor + ">" + RexseeProgressCover.second2string(timeout) + "</font>"));
		mTotal.setTextColor(color);
		mTotal.setClickable(false);
		mTotal.setPadding(10, 10, 10, 0);
		if (!showTotal) mTotal.setVisibility(View.GONE);
		mElapsed = new Chronometer(mContext);
		mElapsed.setTextSize(24);
		mElapsed.setPadding(10, 5, 10, 5);
		mElapsed.setTextColor(color);
		mElapsed.setTypeface(null, Typeface.BOLD);
		mElapsed.setGravity(gravity);
		mElapsed.setClickable(false);
		if (!showElapse) mElapsed.setVisibility(View.GONE);
		mRest = new TextView(mContext);
		mRest.setGravity(gravity);
		mRest.setTextColor(color);
		mRest.setPadding(10, 0, 10, 5);
		mRest.setClickable(false);
		if (!showRest) mRest.setVisibility(View.GONE);
		mLabel = new TextView(mContext);
		mLabel.setGravity(gravity);
		mLabel.setText(Html.fromHtml(styleSheet.label));
		mLabel.setTextColor(color);
		mLabel.setPadding(10, 0, 10, 10);
		mLabel.setClickable(false);

		mContainer = new LinearLayout(mContext);
		mContainer.setClickable(false);
		mContainer.setOrientation(LinearLayout.VERTICAL);
		mContainer.setGravity(Gravity.CENTER);
		mContainer.addView(mTotal, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
		mContainer.addView(mElapsed, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
		mContainer.addView(mRest, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
		mContainer.addView(mLabel, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

		this.addChild(mContainer);

		mMeter = new Chronometer(mContext);
		mMeter.setVisibility(View.GONE);
		mMeter.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
			@Override
			public void onChronometerTick(Chronometer c) {
				String str = c.getText().toString();
				int elapsedTime = RexseeProgressCover.string2second(str);
				if (elapsedTime < 0) {
					mBrowser.exception("Chronomater", "Error:" + str);
				} else if (timeout > 0 && elapsedTime >= timeout) {
					dismiss();
					if (mTimeOutRunnable != null) mTimeOutRunnable.run();
				} else {
					int restTime = timeout - elapsedTime;
					mElapsed.setText(Html.fromHtml("<font color=" + highlightColor + ">" + RexseeProgressCover.second2string(elapsedTime) + "</font>"));
					mRest.setText(Html.fromHtml(RexseeLanguage.PREFIX_TIME_REST + "<font color=" + highlightColor + ">" + RexseeProgressCover.second2string(restTime) + "</font>"));
				}
			}
		});

		this.addChild(mMeter);
		mMeter.start();

	}

}
